{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:27:06.451163Z",
     "start_time": "2019-06-03T05:27:06.429226Z"
    }
   },
   "outputs": [],
   "source": [
    "# import tensorflow.keras.layers as KL\n",
    "# from tensorflow.keras.initializers import Initializer\n",
    "# import tensorflow.keras.backend as K\n",
    "# import tensorflow as tf\n",
    "# import numpy as np\n",
    "# import tensorflow.keras.models as KM\n",
    "\n",
    "# class ConvKernalInitializer(Initializer):\n",
    "#     def __call__(self, shape, dtype=K.floatx(), partition_info=None):\n",
    "#         \"\"\"Initialization for convolutional kernels.\n",
    "\n",
    "#         The main difference with tf.variance_scaling_initializer is that\n",
    "#         tf.variance_scaling_initializer uses a truncated normal with an uncorrected\n",
    "#         standard deviation, whereas here we use a normal distribution. Similarly,\n",
    "#         tf.contrib.layers.variance_scaling_initializer uses a truncated normal with\n",
    "#         a corrected standard deviation.\n",
    "\n",
    "#         Args:\n",
    "#         shape: shape of variable\n",
    "#         dtype: dtype of variable\n",
    "#         partition_info: unused\n",
    "\n",
    "#         Returns:\n",
    "#         an initialization for the variable\n",
    "#         \"\"\"\n",
    "#         del partition_info\n",
    "#         kernel_height, kernel_width, _, out_filters = shape\n",
    "#         fan_out = int(kernel_height * kernel_width * out_filters)\n",
    "#         return tf.random_normal(\n",
    "#             shape, mean=0.0, stddev=np.sqrt(2.0 / fan_out), dtype=dtype)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:27:33.372874Z",
     "start_time": "2019-06-03T05:27:33.342957Z"
    }
   },
   "outputs": [],
   "source": [
    "# x = KL.Input(shape=(32,32,3))\n",
    "# conv = KL.Conv2D(\n",
    "#     8,\n",
    "#     kernel_size=[1, 1],\n",
    "#     strides=[1, 1],\n",
    "#     kernel_initializer=ConvKernalInitializer(),\n",
    "#     padding='same',\n",
    "#     use_bias=True\n",
    "# )\n",
    "# y = conv(x)\n",
    "\n",
    "# model = KM.Model(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:32:43.201808Z",
     "start_time": "2019-06-03T05:32:43.191837Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append('..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:32:43.220788Z",
     "start_time": "2019-06-03T05:32:43.205799Z"
    }
   },
   "outputs": [],
   "source": [
    "batch_size = 32\n",
    "\n",
    "width = 150\n",
    "height = 150\n",
    "input_shape = (height, width, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:33:04.142265Z",
     "start_time": "2019-06-03T05:32:43.222752Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From c:\\users\\hasee\\appdata\\local\\programs\\python\\python35\\lib\\site-packages\\tensorflow\\python\\ops\\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From c:\\users\\hasee\\appdata\\local\\programs\\python\\python35\\lib\\site-packages\\tensorflow\\python\\framework\\function.py:1007: calling Graph.create_op (from tensorflow.python.framework.ops) with compute_shapes is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Shapes are always computed; don't use the compute_shapes as it has no effect.\n",
      "WARNING:tensorflow:From ..\\efficientnet\\layers.py:30: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Deprecated in favor of operator or tf.math.divide.\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.python.keras.applications.imagenet_utils import decode_predictions\n",
    "\n",
    "from efficientnet import EfficientNetB0 as Net\n",
    "from efficientnet import center_crop_and_resize, preprocess_input\n",
    "model = Net(weights=None, include_top=False, input_shape=(336, 336, 3))\n",
    "fname = \"I:/Users/hasee/.keras/models/efficientnet-b0_imagenet_1000_notop.h5\"\n",
    "model.load_weights(fname)\n",
    "model.save(\"../models/conv_base_336_b0.h5\", include_optimizer=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:33:19.972872Z",
     "start_time": "2019-06-03T05:33:16.028327Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "\n",
    "from efficientnet.layers import Swish, DropConnect\n",
    "from efficientnet.model import ConvKernalInitializer\n",
    "from tensorflow.keras.utils import get_custom_objects\n",
    "\n",
    "get_custom_objects().update({\n",
    "    'ConvKernalInitializer': ConvKernalInitializer,\n",
    "    'Swish': Swish,\n",
    "    'DropConnect':DropConnect\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:33:27.171082Z",
     "start_time": "2019-06-03T05:33:19.975769Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From c:\\users\\hasee\\appdata\\local\\programs\\python\\python35\\lib\\site-packages\\tensorflow\\python\\ops\\resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From c:\\users\\hasee\\appdata\\local\\programs\\python\\python35\\lib\\site-packages\\tensorflow\\python\\framework\\function.py:1007: calling Graph.create_op (from tensorflow.python.framework.ops) with compute_shapes is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Shapes are always computed; don't use the compute_shapes as it has no effect.\n",
      "WARNING:tensorflow:From ..\\efficientnet\\layers.py:30: div (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Deprecated in favor of operator or tf.math.divide.\n",
      "WARNING:tensorflow:No training configuration found in save file: the model was *not* compiled. Compile it manually.\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.keras.models import load_model\n",
    "model = load_model(\"../models/conv_base_336_b0.h5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-06-03T05:57:27.456684Z",
     "start_time": "2019-06-03T05:57:27.439730Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'efficientnet'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.path.split(file)[-1]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
